﻿using System;
using System.Globalization;
using System.Linq;
using System.Web;

namespace SW.Lite.Infrastructure.Helpers
{
    /// <summary>
    ///     Class RequestHelper.
    /// </summary>
    public class RequestHelper
    {
        private static RequestHelper _instance;

        /// <summary>
        ///     Gets the instance.
        /// </summary>
        public static RequestHelper Instance
        {
            get { return _instance ?? (_instance = new RequestHelper()); }
        }

        /// <summary>
        ///     GET方式获取Int型数值
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>System.Int32.</returns>
        public int QueryInt(string name, int defaultValue)
        {
            var str = QueryString(name);
            if (String.IsNullOrEmpty(str))
                return defaultValue;

            int v;
            return Int32.TryParse(str, out v) ? v : defaultValue;
        }

        /// <summary>
        ///     Queries the unique identifier.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>Guid.</returns>
        public Guid QueryGuid(string name)
        {
            var str = QueryString(name);
            if (String.IsNullOrEmpty(str))
                return Guid.Empty;

            Guid v;
            return Guid.TryParse(str, out v) ? v : Guid.Empty;
        }

        /// <summary>
        ///     Queries the string.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>System.String.</returns>
        public string QueryString(string name)
        {
            var str = HttpContext.Current.Request.QueryString[name];
            return String.IsNullOrEmpty(str) ? String.Empty : str;
        }

        /// <summary>
        ///     Forms the int.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>System.Int32.</returns>
        public int FormInt(string name, int defaultValue)
        {
            var str = FormString(name);
            if (String.IsNullOrEmpty(str))
                return defaultValue;

            int v;
            return Int32.TryParse(str, out v) ? v : defaultValue;
        }

        /// <summary>
        ///     Forms the unique identifier.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>Guid.</returns>
        public Guid FormGuid(string name)
        {
            var str = FormString(name);
            if (String.IsNullOrEmpty(str))
                return Guid.Empty;

            Guid v;
            return Guid.TryParse(str, out v) ? v : Guid.Empty;
        }

        /// <summary>
        ///     Forms the string.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>System.String.</returns>
        public string FormString(string name)
        {
            var str = HttpContext.Current.Request.Form[name];
            return String.IsNullOrEmpty(str) ? String.Empty : str;
        }

        /// <summary>
        ///     Forms the object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>T.</returns>
        public T FormObject<T>()
        {
            var type = typeof (T);
            var props = type.GetProperties();
            var obj = Activator.CreateInstance<T>();
            foreach (var pi in props)
            {
                var v = FormString(pi.Name);
                if (String.IsNullOrEmpty(v))
                    continue;

                var pType = pi.PropertyType;
                if (pType.IsEnum)
                    pi.SetValue(obj, Enum.ToObject(pType, v), null);
                else
                {
                    if (pType == typeof (Guid))
                    {
                        Guid guidV;
                        if (Guid.TryParse(v, out guidV))
                            pi.SetValue(obj, guidV, null);
                        continue;
                    }
                    if (pType.IsGenericType && pType.Name.StartsWith("Nullable"))
                        pType = Nullable.GetUnderlyingType(pType);
                    pi.SetValue(obj, Convert.ChangeType(v, pType), null);
                }
            }
            return obj;
        }

        /// <summary>
        ///     Users the ip.
        /// </summary>
        /// <returns>System.String.</returns>
        public string IP()
        {
            var isErr = false;
            var ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null
                ? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(CultureInfo.InvariantCulture)
                : HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString(
                    CultureInfo.InvariantCulture);

            if (ip.Length > 15)
                isErr = true;
            else
            {
                var temp = ip.Split('.');
                if (temp.Length == 4)
                {
                    foreach (var t in temp.Where(t => t.Length > 3))
                        isErr = true;
                }
                else
                    isErr = true;
            }

            return isErr ? "1.1.1.1" : ip;
        }
    }
}